#!/bin/sh

# Expand initramfs.cpio.gz
# Author: Julien Chauveau <julien.chauveau@neo-technologies.fr>

# Usage: unmkcpiogz <initramfs.cpio.gz>

if [ $# != 1 ] || [ ! -f $1 ]; then
  echo "Usage: ${0##*/} <initramfs.cpio.gz>" ; exit 1
fi

IMG=$(cd $(dirname $1); pwd)/$(basename $1)
DIR=${IMG%.cpio.gz}
DIR=${DIR%.img}

( gunzip -t $IMG ) || exit 1

if [ "$DIR" = "$IMG" ]; then
  DIR="$IMG-extracted"
fi

if [ -e $DIR ]; then
  echo "Error: $DIR already exists" ; exit 1
fi

mkdir -p $DIR && cd $DIR
gunzip -c $IMG | sudo cpio -i -d -H newc --no-absolute-filenames

echo "Archive extracted: $DIR"
